var express = require('express');
let db = require('../tools/db');
let uuid = require('uuid');
let md5 = require('md5');
let {secret} = require('../tools/config');
let to = require('../tools/to');
let jwt = require('jsonwebtoken');
let intercept = require("../tools/intercept");

var router = express.Router();

/* 注册接口 */
router.post('/register',async function(req, res, next) {
  let {username,password} = req.body;
  if(!username || !password){
    res.json({code:208,data:null,msg:"账号密码不能为空"})
    return;
  }
  //检查账号是否重复注册
  let sql = `select * from e_member where username=?`;
  let a = await db.query(sql,[username]);
  if(a.length>0){
    res.json({code:20002,data:null,msg:"该账号已注册"});
    return;
  }
  //继续注册流程
  let mid = uuid.v1();
  let newPwd = md5(password+secret);
  let sql2 = `insert into e_member(mid,username,password,createdate) values(?,?,?,?)`;
  let [err,result] = await to(db.query(sql2,[mid,username,newPwd,Date.now()]));
  if(!err){
    if(result.affectedRows>0){
      res.json({code:200,data:null,msh:"注册成功"});
      return;
    }else{
      res.json({code:201,data:null,msg:"注册失败"});
    }
    return;
  }else{
    res.json({code:500,data:err,msg:"数据库查询失败，请检查"});
    return;
  }

});

//登录接口
router.post('/login',async (req,res)=>{
  let {username,password} = req.body;
  if(!username || !password){
    res.json({code:208,data:null,msg:"账号密码不能为空"})
    return;
  }
  let sql = `select mid,realname,username,birthdate,sex,head_photo_url from e_member where username=? and password=?`;
  let newPwd = md5(password+secret);
  let [err,result] = await to(db.query(sql,[username,newPwd]));
  if(!err){
    if(result.length>0){
      let payload = {exp:Date.now()+1000*60*60*2};
      let token = jwt.sign(payload,secret);
      //登陆成功后，用户的token、昵称、性别、生日、头像等信息都一般要返回客户端 供给前端开发使用
      let [{mid,realname,username,birthdate,sex,head_photo_url}] = result;
      res.json({code:200,data:{token,mid,realname,username,birthdate,sex,head_photo_url},msg:"登陆成功"})
    }else{
      res.json({code:20001,data:null,msg:"账号或密码错误"})
    }
  }else{
    res.json({code:500,data:err,msg:"数据库查询失败，请检查"});
    return;
  }

})

router.use(intercept());
//获取会员信息
router.get('/user',async (req,res)=>{
  let mid = req.query.mid;
  let sql = `SELECT
	mid,
	realname,
	username,
	sex,
	birthdate,
	head_photo_url,
	createdate
FROM
	e_member 
WHERE
	mid = ?`
  let a = await db.query(sql,[mid]);
  res.json({code:200,data:a[0],msg:"会员信息获取成功"})
})

//更新会员信息
router.put('/user',async (req,res)=>{
  let {mid,realname,username,sex,birthdate} = req.body;
  if(!mid || !username){
    res.json({code:201,data:null,msg:"mid或账号不能为空"})
    return;
  }
  let sql = `update e_member set realname=?,username=?,sex=?,birthdate=? where mid=? `;
  let a = await db.query(sql,[realname,username,sex,birthdate,mid])
  if(a.affectedRows>0){
    //更新成功后，更新后的信息也返回给前端备用
    res.json({code:200,data:{realname,username,sex,birthdate},msg:"用户信息更新成功"})
  }else{
    res.json({code:201,data:null,msg:"用户信息更新失败，请检查参数"})
  }

})

//更新用户图像
router.put('/uploadhead',async (req,res)=>{
  
})

module.exports = router;
